import socket
import sys
import select


def tcp_client():
    if len(sys.argv)==1:
        return 
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    caddr = (sys.argv[1],5888)
    client.connect(caddr)
    epoll = select.epoll()
    epoll.register(client.fileno(), select.EPOLLIN)
    epoll.register(sys.stdin.fileno(), select.EPOLLIN)

    while True:
        events = epoll.poll()
        for fd,event in events:
            if fd == sys.stdin.fileno():
                try:
                    message = input()
                    client.send(message.encode('utf-8'))
                except EOFError:
                    print('退退')
                    exit()
            elif fd == client.fileno():
                data = client.recv(100)
                if data:
                    print(data.decode('utf-8'))
                else:
                    return
    client.close()


if __name__ == '__main__':
    tcp_client()

